package com.asia5b.a5mall.order.activity

import androidx.recyclerview.widget.LinearLayoutManager
import android.text.TextUtils
import android.view.View
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.asia5b.a5bmall.resource.ActCons
import com.asia5b.a5mall.middleware.base.SimpleActivity
import com.asia5b.a5mall.middleware.event.OrderRefundEvent
import com.asia5b.a5mall.middleware.logic.viewmodel.order.AftersaleDetailActivitVM
import com.asia5b.a5mall.middleware.model.order.AftersaleDetail2Model
import com.asia5b.a5mall.middleware.model.bean.MenuBean
import com.asia5b.a5mall.middleware.model.shop.AbsGoodsModel
import com.asia5b.a5mall.middleware.utils.*
import com.asia5b.a5mall.middleware.widget.CancelSureViewModel
import com.asia5b.a5mall.order.R
import com.asia5b.a5mall.order.adapter.AfterDetailGoodsAdapter
import com.asia5b.a5mall.order.adapter.AftersaleDetailAdapter
import com.asia5b.a5mall.order.databinding.ActivityAftersaledetail2Binding
import kotlinx.android.synthetic.main.activity_aftersaledetail2.*
import org.greenrobot.eventbus.EventBus

/**
 * 申请售后详情
 */
@Route(path = ActCons.AftersaleDetailActivity)
class AftersaleDetailActivity : SimpleActivity<AftersaleDetailActivitVM, ActivityAftersaledetail2Binding>() {
    override val viewModelClazz: Class<AftersaleDetailActivitVM>
        get() = AftersaleDetailActivitVM::class.java

    override fun getDataBinding(): ActivityAftersaledetail2Binding {
        sn = intent.getStringExtra("sn")!!
        return ActivityAftersaledetail2Binding.inflate(layoutInflater)
    }
    override var sIsDark = true

    var sn: String = ""

    val mRecyclerView by lazy { rv_aftersaledetail_price }
    val mDatas = arrayListOf<MenuBean>()
    val mAdapter by lazy { AftersaleDetailAdapter(mDatas) }
    val mRecyclerViewGoods by lazy { rv_aftersaledetail_goods }
    val mDatasGoods = arrayListOf<AbsGoodsModel>()
    val mAdapterGoods by lazy { AfterDetailGoodsAdapter(mDatasGoods) }

    override fun initView() {
        mRecyclerView.run {
            layoutManager = LinearLayoutManager(mActivity)
            adapter = mAdapter
        }
        mRecyclerViewGoods.run {
            layoutManager = LinearLayoutManager(mActivity)
            adapter = mAdapterGoods
        }
        mViewModel.getAftersaleDetail(sn)
    }

    override fun initEvent() {

    }

    override fun startObserver() {
        mViewModel.getAftersaleDetailResultLiveData.observe(this, Observer {
            getAftersaleDetailResult(it)
        })
        mViewModel.cancelRefundResultLiveData.observe(this, Observer {
            cancelRefundResult(it)
        })
    }

    override fun initData() {
    }

     fun getAftersaleDetailResult(aftersaleDetail2Model: AftersaleDetail2Model) {
        mDatas.clear()
        aftersaleDetail2Model.refund!!.run {
            mDatas.add(MenuBean(1, getString(R.string.售后单号), sn))
            val create_time = WTimeUtils.format("${create_time * 1000}", WTimeUtils.text_yyyy_MM_dd_HH_mm_ss)
            mDatas.add(MenuBean(getString(R.string.申请时间), create_time))
            mDatas.add(MenuBean(getString(R.string.售后类型), refuse_type_text))
            mDatas.add(MenuBean(getString(R.string.售后状态), refund_status_text))
            mDatas.add(MenuBean(getString(R.string.售后原因), refund_reason))
            mDatas.add(MenuBean(getString(R.string.详细描述), customer_remark))
            mDatas.add(MenuBean(getString(R.string.退款方式), account_type_text))
            val priceText = CurrencyPriceBuilder3().run {
                price = refund_price
                this.priceList = refund_price_list
                buildPriceText()
            }
            mDatas.add(MenuBean(8, getString(R.string.退款金额), priceText))

            tv_aftersaledetail_totalprice.text = "${getString(R.string.退款金额)}: ${priceText}"

            //是否显示申请退款按钮
            ll_aftersaledetail_submit.visibility = (TextUtils.equals(aftersaleDetail2Model.refund?.refund_status, "APPLY")).judge(View.VISIBLE, View.GONE)

            mDataBinding.model = this

            bt_aftersaledetail_submit.isEnabled = true
            bt_aftersaledetail_submit.setOnClickListener {
                CancelSureViewModel(mActivity).apply {
                    leftStringRes = R.string.确定
                    rightStringRes = R.string.取消
                    contentStringRes = R.string.确认取消申请退款吗
                    leftClick = {
                        mViewModel.cancelRefund(sn)
                    }
                }.show()
            }
        }
        mAdapter.notifyDataSetChanged()
        mDatasGoods.clear()
        mDatasGoods.addAll(aftersaleDetail2Model.refund_goods ?: arrayListOf())
        mAdapterGoods.notifyDataSetChanged()
        tv_aftersaledetail_goodsnum.text = String.format(getString(R.string.共_数_件商品), "${mDatasGoods.size}")

    }

     fun cancelRefundResult(orderSn: String) {
         getEventBus().post(OrderRefundEvent(orderSn))
        pop()
    }

}